package cn.hou.java.designpattern._11composite;

import java.util.List;

public class Test01 {
	public static void main(String[] args) {
		Directory root=new Directory("D盘");
		//添加子节点
		Directory music=new Directory("音乐");
		music.add(new File("童话.mp3"));
		Directory jay = new Directory("周杰伦");
		jay.add(new File("七里香.mp3"));
		music.add(jay);
		root.add(music);
		Directory book=new Directory("书籍");
		book.add(new File("java编程思想.pdf"));
		Directory xiaoshuo = new Directory("小说");
		xiaoshuo.add(new File("逆天邪神.txt"));
		book.add(xiaoshuo);
		root.add(book);
		//访问整个对象树
		displayTree(root,0);
	}
	
	//通过根节点访问整个对象树
	public static void displayTree(IFile root,int deep){
		//层级符号,根节点不打印,子节点打印,先打印,再访问
		for (int i = 0; i < deep; i++) {
			System.out.print("--");
		}
		root.display();
		List<IFile> child = root.getChild();//获取子节点并遍历
		for (IFile file : child) {
			if(file instanceof File){ //如果是文件
				for (int i = 0; i <= deep; i++) {//打印层级符,此处有=,比他上一级节点多打印--
					System.out.print("--");
				}
				file.display();
			}else{
				displayTree(file,deep+1);//递归遍历子节点,deep+1: 层级符往后打印
			}
		}
	}
}
